package com.rh.controller;


import com.rh.Enums;
import com.rh.dto.Page;
import com.rh.dto.Result;
import com.rh.entity.Attendance;
import com.rh.excrptions.DoException;
import com.rh.service.AttendanceService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 考勤
 */
@CrossOrigin
@Controller
@ResponseBody
//管理者和平台管理员访问
@RequiresRoles(value = {"manager","admin"},logical = Logical.OR)
public class AttendanceController {
    @Autowired
    private AttendanceService attendanceService;

    /**
     * 请假加班
     */
    //认证才能访问
    @RequiresAuthentication
    @RequestMapping(value = "/add_attendance",method = RequestMethod.POST)
    public Result addAttendance(Attendance attendance){
        try{
            System.out.println("添加=====》"+attendance);
            //int i=1/0;
            attendanceService.addMethod(attendance);
            Result result = new Result();
            return  result;
        }catch (Exception e){
            throw new DoException(Enums.ADD_ERROR);
        }
    }
    /**
     * 更新信息
     */
    //认证才能访问
    @RequiresAuthentication
    @RequestMapping(value = "/update_attendance",method = RequestMethod.POST)
    public Result updateAttendance(Attendance attendance){
        try{
            System.out.println("更新====>"+attendance);
            attendanceService.updateMethod(attendance);
            Result result = new Result();
            return result;
        }catch (Exception e){
            throw new DoException(Enums.UPDATE_ERROR);
        }
    }
    /**
     * 删除信息
     */
    //认证才能访问
    @RequiresAuthentication
    @RequestMapping(value = "/delete_attendance",method = RequestMethod.GET)
    public Result deleteAttendance(Integer attendanceId){
        try {
            attendanceService.deleteMethod(attendanceId);
            Result result = new Result();
            return result;
        }catch (Exception e){
            throw new DoException(Enums.DELETE_ERROR);
        }
    }
    /**
     * 分页
     */

    @RequiresAuthentication
    @RequestMapping(value = "/getAttendancePage",method = RequestMethod.GET)
    public Result getAttendancePage(Integer pageNo,Integer pageSize) {
        try {
            if (pageNo == null) {
                pageNo = 1;
            }
            if (pageSize == null) {
                pageSize = 3;
            }
            Page attendancePage = attendanceService.getAttendancePage(pageNo, pageSize);
            Result result = new Result();
            result.setData(attendancePage);
            return result;
        }catch(Exception e){
            throw new DoException(Enums.GETLIST_ERROR);
        }
    }
    /**
     * 根据id查询信息
     */
    //认证才能访问
    @RequiresAuthentication
    @RequestMapping(value = "/get_attendanceById",method = RequestMethod.GET)
    public Result getAttendanceById(Integer attendanceId){
       try {
           Attendance methodAllById = attendanceService.getMethodAllById(attendanceId);
           Result result = new Result();
           result.setData(methodAllById);
           return result;
       }catch (Exception e){
           throw new DoException(Enums.GETLIST_ERROR);
       }
       }
}
